package com.ruoyi.web.controller.admin.newSystem;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MenuDO;
import com.ruoyi.common.core.domain.permission.vo.menu.MenuCreateReqVO;
import com.ruoyi.common.core.domain.permission.vo.menu.MenuListReqVO;
import com.ruoyi.common.core.domain.permission.vo.menu.MenuUpdateReqVO;
import com.ruoyi.common.enums.CommonStatusEnum;
import com.ruoyi.newSystem.service.MenuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;


/**
 * The type Menu controller.
 */
@Tag(name = "管理后台 - 菜单")
@RestController
@RequestMapping("/newSystem/menu")
@Validated
public class MenuController {

    @Resource
    private MenuService menuService;



    /**
     * 创建菜单
     *
     * @param reqVO the req vo
     * @return the common result
     */
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody MenuCreateReqVO reqVO) {
        Long menuId = menuService.createMenu(reqVO);
        return success(menuId);
    }

    /**
     * 更新菜单
     *
     * @param reqVO the req vo
     * @return the common result
     */
    @PutMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody MenuUpdateReqVO reqVO) throws Exception {
        menuService.updateMenu(reqVO);
        return success(true);
    }

    /**
     * 删除菜单
     *
     * @param id 菜单编号
     * @return the common result
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Long id) throws Exception {
        menuService.deleteMenu(id);
        return success(true);
    }

    /**
     * 获得所有菜单列表
     *
     * @param reqVO the req vo
     * @return 菜单列表 menu list
     */
    @GetMapping("/index")
    public AjaxResult index(MenuListReqVO reqVO) {
        List<MenuDO> list = menuService.getMenuList(reqVO);
        list.sort(Comparator.comparing(MenuDO::getSort));
        return success(list);
    }

    /**
     * 通过菜单类型获取菜单
     *
     * @param reqVO
     * @return 通过类型过滤的菜单
     */
    @GetMapping("/getMenuListByType")
    public AjaxResult getMenuListByType(MenuListReqVO reqVO) {
        List<MenuDO> list = menuService.getMenuListByType(reqVO);
        list.sort(Comparator.comparing(MenuDO::getSort));
        return success(list);
    }

    /**
     * 获得菜单
     *
     * @param id the id
     * @return the menu
     */
    @GetMapping("/detail")
    public AjaxResult detail(Long id) {
        MenuDO menu = menuService.getMenu(id);
        return success(menu);
    }

    /**
     * 根据用户id 获取所有菜单
     *
     * @param userId 用户id
     * @return 用户所有菜单
     */
    @GetMapping("/getMenusByUserId")
    public AjaxResult getMenusByUserId(Long userId) {
        List<MenuDO> menusByUserId = menuService.getMenusByUserId(userId);
        return success(menusByUserId);
    }

    /**
     * 获取简单菜单列表。
     *
     * @return the simple menu list
     */
    @GetMapping("/listAllSimple")
    public AjaxResult getSimpleMenuList() {
        // 获得菜单列表，只要开启状态的
        MenuListReqVO reqVO = new MenuListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        // 改为调用已存在的方法
        List<MenuDO> list = menuService.getMenuList(reqVO);

        // 排序后，返回给前端
        list.sort(Comparator.comparing(MenuDO::getSort));
        return success(list);
    }





}
